package cn.t.user.converter;

import cn.t.vo.user.FirmVO;
import cn.t.vo.user.IndividualVO;
import cn.t.vo.user.PassportVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author l
 */
@Slf4j
@Component
public class UserQueryVOConverter {

    public IndividualVO individualJson(String individualJson) {
        if (StringUtils.isEmpty(individualJson)) {
            return null;
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(individualJson, IndividualVO.class);
        } catch (Exception e) {
            log.error("combineSession error, individualJson={}", individualJson, e);
        }

        return null;
    }

    public PassportVO convertPassport(String passportJson) {
        if (StringUtils.isEmpty(passportJson)) {
            return null;
        }
        try {

            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(passportJson, PassportVO.class);
        } catch (Exception e) {
            log.error("combineSession error, passportJson={}", passportJson, e);
        }

        return null;
    }

    public List<FirmVO> convertFirms(String participantJson) {
        if (StringUtils.isEmpty(participantJson)) {
            return null;
        }

        try {
            ObjectMapper mapper = new ObjectMapper();
            JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, FirmVO.class);
            return mapper.readValue(participantJson, type);
        } catch (Exception e) {
            log.error("combineSession error, participantJson={}", participantJson, e);
        }

        return null;
    }

}
